package io.chenglicun.modules.knowledge.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.chenglicun.common.service.impl.CrudServiceImpl;
import io.chenglicun.modules.knowledge.dao.KnowledgeExtraDao;
import io.chenglicun.modules.knowledge.dto.KnowledgeExtraDTO;
import io.chenglicun.modules.knowledge.entity.KnowledgeExtraEntity;
import io.chenglicun.modules.knowledge.entity.KnowledgeQaEntity;
import io.chenglicun.modules.knowledge.service.KnowledgeExtraService;
import io.chenglicun.modules.knowledge.service.KnowledgeQaService;
import io.chenglicun.modules.security.user.SecurityUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 知识库额外固定问题配置
 *
 * @author shicong 705740711@qq.com
 * @since 1.0 2024-05-10
 */
@Service
public class KnowledgeExtraServiceImpl extends CrudServiceImpl<KnowledgeExtraDao, KnowledgeExtraEntity, KnowledgeExtraDTO> implements KnowledgeExtraService {

    @Autowired
    private KnowledgeQaService knowledgeQaService;

    @Override
    public QueryWrapper<KnowledgeExtraEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<KnowledgeExtraEntity> wrapper = new QueryWrapper<>();
        String hasPublish = (String) params.get("hasPublish");
        wrapper.eq(StrUtil.isNotBlank(hasPublish), "has_publish", Boolean.parseBoolean(hasPublish));

        String contentType = (String) params.get("contentType");
        wrapper.eq(StrUtil.isNotBlank(contentType), "content_type", contentType);

        String type = (String) params.get("type");
        wrapper.eq(StrUtil.isNotBlank(type), "type", type);

        String title = (String) params.get("title");
        wrapper.like(StrUtil.isNotBlank(title), "title", title);

        return wrapper;
    }


    @Override
    public KnowledgeExtraDTO getQaExtraInfo(Long id) {
        KnowledgeExtraDTO knowledgeExtraDTO = this.get(id);
        KnowledgeQaEntity knowledgeQaEntity = new KnowledgeQaEntity();
        knowledgeQaEntity.setQuestion(knowledgeExtraDTO.getTitle());
        knowledgeQaEntity.setAnswer(knowledgeExtraDTO.getContent());
        knowledgeQaEntity.setType("1");
        knowledgeQaEntity.setCreator(SecurityUser.getUserId());
        knowledgeQaEntity.setContentType(knowledgeExtraDTO.getContentType());
        knowledgeQaService.insert(knowledgeQaEntity);
        return knowledgeExtraDTO;
    }
}